package edu.umassd.visual;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.swing.AbstractButton;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;

import org.springframework.beans.factory.annotation.Autowired;

import edu.umassd.visual.options.XAxisSelector;
import edu.umassd.visual.options.YAxisSelector;

/**
 * Creates an option panel that can be used to enable features in the
 * {@link Display}.
 * 
 * @author Dan Avila
 * 
 */
@org.springframework.stereotype.Component
public class OptionPanel extends JPanel
{
	private static final long serialVersionUID = 8767703092538265261L;

	@Autowired
	private Map<String, ActionListener> options;

	private Box box = Box.createVerticalBox();

	@Autowired
	private XAxisSelector x;

	@Autowired
	private YAxisSelector y;

	@PostConstruct
	protected void init()
	{
		createButton("Save", options.get("save"));
		createButton("Load", options.get("load"));
		createButton("Fit", options.get("fit"));
		createToggleOn("Zoom", options.get("toggleZoom"));
		createToggleOn("Cursor Coordinates", options.get("toggleCoords"));
		// createToggle("Editor", options.get("toggleEditor"));
		createButton("Font", options.get("font"));
		createButton("Exit", options.get("exit"));

		createTextField("Dataset Width: ", options.get("width"));
		createTextField("Dataset Height: ", options.get("height"));

		createCombo("X-Axis: ", x);
		createCombo("Y-Axis: ", y);

		add(box);
	}

	private void createCombo(String title, JComboBox<?> combo)
	{
		this.box.add(new JLabel(title));
		this.box.add(combo);
	}

	private void createTextField(String string, ActionListener actionListener)
	{
		Box cont = Box.createVerticalBox();

		JTextField field = new JTextField(10);
		field.addActionListener(actionListener);

		cont.add(new JLabel(string));
		cont.add(field);
		box.add(cont);
	}

	private void createButton(String name, ActionListener listener)
	{
		JButton button = new JButton();

		configureButton(button, name, listener);
	}

	private void createToggleOn(String name, ActionListener listener)
	{
		createToggle(name, listener).doClick();
	}

	private JToggleButton createToggle(String name, ActionListener listener)
	{
		JToggleButton button = new JToggleButton();

		configureButton(button, name, listener);

		return button;
	}

	private void configureButton(AbstractButton button, String text,
			ActionListener... listeners)
	{
		button.setText(text);

		if (listeners != null)
		{
			for (ActionListener listener : listeners)
			{
				button.addActionListener(listener);
			}
		}

		button.setAlignmentX(Component.CENTER_ALIGNMENT);

		box.add(button);
		box.add(Box.createVerticalStrut(10));
	}
}
